package com.e3roid.examples;

import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.sprite.TextSprite;
import com.e3roid.util.FPSListener;

public class LabelReplaceExample extends E3Activity implements FPSListener {

	private final static int WIDTH  = 320;
	private final static int HEIGHT = 480;
	
	private TextSprite label;

	@Override
	public E3Engine onLoadEngine() {
		E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);
		engine.requestFullScreen();
		engine.requestPortrait();
		
		engine.getFPSCounter().setDurationMsec(1000);
		engine.getFPSCounter().addListener(this);		
		return engine;
	}

	@Override
	public E3Scene onLoadScene() {
		E3Scene scene = new E3Scene();
		scene.setBackgroundColor(0.94f, 1.00f, 0.94f, 1);
		
		// Label width and height cannot be determined before loading Label object.
		// So if you want to move the label relative to the label size, use move() method
		// after creating Label instance.
		label.move((getWidth() - label.getWidth()) / 2, (getHeight() - label.getHeight()) / 2);
		
		scene.getTopLayer().add(label);
		return scene;
	}

	@Override
	public void onLoadResources() {
		// Set temporary text until receiving the real FPS value.
		label = new TextSprite("FPS: 00.00", 36, this);
		// Hide FPS label so that temporary text should not be shown.
		label.hide();
	}
	
	@Override
	public void onFPS(float fps, float minFPS, float maxFPS) {
		if (label != null) {
			// Reload the label without resizing the label.
			// If the text length has been changed, reload(true) needs to be called to resize the label.
			// The Label's reload(true) method re-creates OpenGL buffers to resize the label.
			label.setText(String.format("FPS: %.2f", fps));
			
			// Now the label value is valid, show the label;
			if (!label.isVisible()) label.show();
		}
	}
}